package com.cjkj.system.service.impl;

import com.cjkj.common.core.constant.HttpStatus;
import com.cjkj.common.core.constant.WarnConstants;
import com.cjkj.common.core.domain.PatVisitIntervalData;
import com.cjkj.common.core.domain.R;
import com.cjkj.common.core.utils.DateUtils;
import com.cjkj.common.core.utils.bean.BeanUtils;
import com.cjkj.system.api.RemoteWarnService;
import com.cjkj.system.api.domain.APIWarnHandle;
import com.cjkj.system.api.domain.APIWarnRecord;
import com.cjkj.system.api.domain.APIWarnRecords;
import com.cjkj.system.api.domain.request.WarnListReq;
import com.cjkj.system.api.domain.request.WarnRecordReq;
import com.cjkj.system.domain.TPatVisit;
import com.cjkj.system.domain.vo.PatientDetailInfoVo;
import com.cjkj.system.domain.vo.SysWarnVo;
import com.cjkj.system.domain.vo.SysWarnsVo;
import com.cjkj.system.mapper.TPatVisitMapper;
import com.cjkj.system.service.WarnService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class WarnServiceImpl implements WarnService {

    @Resource
    private RemoteWarnService remoteWarnService;
    @Resource
    private TPatVisitMapper patVisitMapper;

    @Override
    public SysWarnsVo listWarn(String hospitalId, String deptId, String hasHandle, String dataType) {
        WarnListReq req = new WarnListReq();
        req.setHospitalId(hospitalId);
        req.setDeptId(deptId);
        req.setHasHandle(hasHandle);
        req.setDataType(dataType);
        R<APIWarnRecords> remoteResult = remoteWarnService.listWarn(req);
        List<SysWarnVo> sysWarnVoList = new ArrayList<>();
        SysWarnsVo sysWarnsVo = new SysWarnsVo();
        if(remoteResult.getCode() == HttpStatus.SUCCESS && remoteResult.getData() != null){
            APIWarnRecords apiData = remoteResult.getData();
            apiData.getWarnRecordList().forEach(e->{
                SysWarnVo sysWarnVo = new SysWarnVo();
                BeanUtils.copyProperties(e,sysWarnVo);
                //根据患者就诊号查询患者就诊信息
                PatientDetailInfoVo patVisitInfo = patVisitMapper.getDetailInfo(e.getVisitNo());
                if(patVisitInfo != null){
                    sysWarnVo.setHospitalId(patVisitInfo.getHospitalId());
                    sysWarnVo.setDeptId(patVisitInfo.getDeptId());
                    sysWarnVo.setDeptName(patVisitInfo.getDeptName());
                    sysWarnVo.setBedNo(patVisitInfo.getBedNo());
                    sysWarnVo.setVisitNo(patVisitInfo.getVisitNo());
                    sysWarnVo.setPatientName(patVisitInfo.getPatientName());
                    sysWarnVoList.add(sysWarnVo);
                }
            });
        }
        if(!sysWarnVoList.isEmpty()){
            List<SysWarnVo> heartWarnList = sysWarnVoList.stream().filter(
                    e -> WarnConstants.WARN_TYPE_HEART.equals(e.getWarnType())
                            || WarnConstants.WARN_TYPE_BPM.equals(e.getWarnType())
                            || WarnConstants.WARN_TYPE_HR.equals(e.getWarnType())
            ).collect(Collectors.toList());
            List<SysWarnVo> spO2WarnList = sysWarnVoList.stream().filter(
                    e -> WarnConstants.WARN_TYPE_SPO2.equals(e.getWarnType()) || WarnConstants.WARN_TYPE_BRACELET_SPO2.equals(e.getWarnType())).collect(Collectors.toList());
            List<SysWarnVo> breathWarnList = sysWarnVoList.stream().filter(
                    e -> WarnConstants.WARN_TYPE_BREATH.equals(e.getWarnType())).collect(Collectors.toList());
            List<SysWarnVo> temperatureWarnList = sysWarnVoList.stream().filter(
                    e -> WarnConstants.WARN_TYPE_TEMPERATURE.equals(e.getWarnType())).collect(Collectors.toList());
            List<SysWarnVo> electricWarnList = sysWarnVoList.stream().filter(
                    e -> WarnConstants.WARN_TYPE_ELECTRIC.equals(e.getWarnType())).collect(Collectors.toList());
            List<SysWarnVo> tj03SbpWarnList = sysWarnVoList.stream().filter(
                    e -> WarnConstants.WARN_TYPE_TJ03_SBP.equals(e.getWarnType())).collect(Collectors.toList());
            List<SysWarnVo> tj03DbpWarnList = sysWarnVoList.stream().filter(
                    e -> WarnConstants.WARN_TYPE_TJ03_DBP.equals(e.getWarnType())).collect(Collectors.toList());
            tj03SbpWarnList.addAll(tj03DbpWarnList);
            sysWarnsVo.setHeartWarn(heartWarnList);
            sysWarnsVo.setSpO2Warn(spO2WarnList);
            sysWarnsVo.setBreathWarn(breathWarnList);
            sysWarnsVo.setTemperatureWarn(temperatureWarnList);
            sysWarnsVo.setElectricWarn(electricWarnList);
            sysWarnsVo.setBpWarn(tj03SbpWarnList);
        }
        return sysWarnsVo;
    }

    @Override
    public List<APIWarnRecord> listWarn(Long visitNo) {

        TPatVisit patVisit = patVisitMapper.selectTPatVisitByVisitNo(visitNo);
        if(patVisit == null) {
            return Collections.emptyList();
        }
        PatVisitIntervalData intervalTime = DateUtils.getIntervalTime(patVisit.getVisitTime(), patVisit.getRentalDays(), patVisit.getOutTime(), patVisit.getDataType());
        Date expectOutTime = intervalTime.getOutTime();

        R<APIWarnRecords> result = remoteWarnService.listWarns(visitNo, DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, expectOutTime));
        if(result.getCode() == HttpStatus.SUCCESS && result.getData() != null){
            return result.getData().getWarnRecordList();
        }
        return null;
    }

    @Override
    public Boolean handleWarn(List<APIWarnHandle> apiWarnHandleList) {
        R<Boolean> result = remoteWarnService.handleWarns(apiWarnHandleList);
        if(result.getCode() == HttpStatus.SUCCESS){
            return result.getData();
        }
        return false;
    }

    @Override
    public Boolean handleWarn(WarnRecordReq req) {
        R<Boolean> result = remoteWarnService.handleWarns(req);
        if(result.getCode() == HttpStatus.SUCCESS){
            return result.getData();
        }
        return false;
    }
}
